package cn.com.cennavi.java;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class WriteRedis_ZHANGJIAKOU {

	static String FileFolder = "C:\\Users\\wangr\\Desktop\\5city\\450200\\";
	static String outFileName = "C:\\Users\\wangr\\Desktop\\5city\\t_speed_pattern_liuzhou2.txt";
	static String FileName="C:\\Users\\wangr\\Desktop\\5city\\t_speed_pattern_liuzhou2.txt";
	static String filePrefix = "congestion_speed_pattern";

	/**
	 * key:congestion_speed_pattern_1_0835 key1:51494040012
	 * value1:0835_51494040012_1_58.2_4_873_54
	 */
	/**
	 * 
	 * @Description
	 * @author buf
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		editSpeedPattern(FileFolder, outFileName);

		gateDataToRedis();
		
	}

	private static void gateDataToRedis() throws IOException {
		// TODO Auto-generated method stub
		JedisCluster jedis = getJedis() ;
		long i = 0;
		HashMap<String, Map<String, String>> dataMap = new HashMap<String, Map<String, String>>();
		BufferedReader br = new BufferedReader(new FileReader(new File(FileName)));
		String line = br.readLine();
		while ((line = br.readLine()) != null) {
			// System.out.println(i++);
			if (i++ % 1000 == 0) {
				System.out.println(i);
			}
			// WEEKDAY TIME RS_ID RS_KIND TRAVEL_TIME DEGREE RS_LENGTH SPEED
			String[] arr = line.split(",");
			String WEEKDAY = arr[0];
			String time = arr[1];
			String rs_id = arr[2];
			String rs_kind = arr[3];
			String travel_time = arr[4];
			String degree = arr[5];
			String rs_length = arr[6];
			String speed = arr[7];

			//
			String key = filePrefix + "_" + WEEKDAY + "_" + time;
			String value1 = time + "_" + rs_id + "_" + degree + "_" + speed + "_" + rs_kind + "_" + rs_length + "_" + travel_time;
			String key1 = rs_id;
//			System.out.println("key:" + key);
//			System.out.println("key1:" + key1);
//			System.out.println("value1:" + value1);
//			Map<String, String> v = dataMap.get(key);
//			if (v == null) {
//				v = new HashMap<String, String>();
//				v.put(key1, value1);
//				dataMap.put(key, v);
//			} else {
//				v.put(key1, value1);
//			}
//		}
//		System.out.println("--------------");
//		System.out.println(dataMap.size());
//		int j = 0;
//		for (Map.Entry<String, Map<String, String>> e : dataMap.entrySet()) {
//			String key = e.getKey();
//			Map<String, String> v = e.getValue();
//			jedis.hmset(key, v);
//			System.out.println(j++);
		}
//		jedis.close();
	}

	private static void editSpeedPattern(String fileFolder2, String fileName2) throws IOException {
		// TODO Auto-generated method stub
		// line="1	0835	51494040012	4	54	1	873	58.2"
		BufferedWriter o=new BufferedWriter(new FileWriter(new File(fileName2)));

		File[] files = new File(fileFolder2).listFiles();
		for (File files1 : files) {
			System.out.println("-----------------------------------------");
			System.out.println(files1.getName());
			String week = files1.getName().replace("W", "");

			File[] files2 = files1.listFiles();
			for (File files3 : files2) {
				System.out.println(files3.getName());
				String time = files3.getName().replace(".csv", "");

				BufferedReader br = new BufferedReader(new FileReader(files3));
				String line = "";
				
				while ((line=br.readLine()) != null) {
					String []arr=line.split(",");
					int mesh=Integer.parseInt(arr[0]);
					int numberId=Integer.parseInt(arr[1]);
					int kind=Integer.parseInt(arr[2]);
					String travelTime=arr[3];
					String los=arr[4];
					String length=arr[5];
					String speed=String.format("%.2f",Double.parseDouble(length)/Double.parseDouble(travelTime)*3.6D);
					String rticId = String.format("%06d%01d%04d", mesh, kind, numberId);		
					String outLine=week+","+time+","+rticId+","+kind+","+travelTime+","+los+","+length+","+speed;
					o.write(outLine);
					o.newLine();
				}
				br.close();
			}

		}
		o.close();

	}

	public static JedisCluster getJedis() {
		Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
		// Jedis Cluster will attempt to discover cluster nodes automatically
		// 老的redis集群
		// jedisClusterNodes.add(new HostAndPort("172.20.10.141", 9001));

		jedisClusterNodes.add(new HostAndPort("172.20.10.75", 9001));
		JedisCluster jedis = new JedisCluster(jedisClusterNodes);
		return jedis;
	}

}
